# coding:utf-8

result = []

with open("report.txt") as f:
    file = f.readlines()

for i in file:
    everyone = i.split()
    new_everyone = everyone[1:]
    sum = 0
    num = 0
    for j in new_everyone:
        sum = sum + int(j)
        num = num + 1
        avg = float(sum) / num
        Avg = "%.1f" % avg
    everyone.append(str(sum))
    everyone.append(str(Avg))
    for m in everyone[1:-2]:
        if int(m) < 60:
            everyone[everyone.index(m)] = "不及格"
    result.append(everyone)

result.sort(key=lambda r : r[-1],reverse = True)
#print result

title = ["名次","姓名","语文","数学","英语","物理","化学","生物","政治","历史","地理","总分","平均分"]
new_title = " ".join(title)
#print new_title

with open("result.txt","a") as final:
    final.write(new_title)
    final.write("\n")

id = 1

for k in result:
    k.insert(0,str(id))
    id = id +1
    x = " ".join(k)
    with open("result.txt","a") as final:
        final.write(x)
        final.write("\n")
